package org.yourscrum.domain;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * @author Jettro Coenradie
 */
@Entity
@Table(name = "story")
public class Story extends AbstractEntity {
    @Column(name = "name")
    private String name;

    @Column(name = "description")
    private String description;

    @OneToMany(mappedBy = "story", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private List<Task> tasks = new ArrayList<Task>();

    @OneToOne
    @JoinColumn(name = "next_story_id")
    private Story nextStory;

    @ManyToOne
    @JoinColumn(name = "backlog_id")
    private Backlog backlog;

    public Story() {
    }

    public Story(String name) {
        this();
        this.name = name;
    }

    public Story(String name, String description) {
        this();
        this.name = name;
        this.description = description;
    }

    public void changeName(String name) {
        this.name = name;
    }

    public void changeDescription(String description) {
        this.description = description;
    }

    public Task addNewTask(String description) {
        Task newTask = new Task(this, description);
        this.tasks.add(newTask);
        return newTask;
    }

    public int getNumberOfTasks() {
        return tasks.size();
    }

    public void planBefore(Story story) {
        nextStory = story;
    }

    public void planAtEnd() {
        nextStory = null;
    }

    /* Getters */
    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public List<Task> getTasks() {
        return Collections.unmodifiableList(tasks);
    }

    public Story nextStory() {
        return nextStory;
    }
}
